package com.yjz.zpay.hook;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;

import com.yjz.zpay.utils.L;
import com.yjz.zpay.utils.PayHelperUtils;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * main hook
 *
 * @author yjz
 *         created at 2019/1/18 下午9:43
 */

public class Main implements IXposedHookLoadPackage {
    public static String ALIPAY_PACKAGE = "com.eg.android.AlipayGphone";
    public static boolean ALIPAY_PACKAGE_ISHOOK = false;

    public static String WECHAT_PACKAGE = "com.tencent.mm";
    public static boolean WECHAT_PACKAGE_ISHOOK = false;


    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam)
            throws Throwable {
        if (lpparam.appInfo == null || (lpparam.appInfo.flags & (ApplicationInfo.FLAG_SYSTEM |
                ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
            return;
        }
        final String packageName = lpparam.packageName;
        final String processName = lpparam.processName;
        if (ALIPAY_PACKAGE.equals(packageName)) {
            L.log("支付宝启动---hook成功");
            try {
                XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        final Context context = (Context) param.args[0];
                        ClassLoader appClassLoader = context.getClassLoader();
                        if (ALIPAY_PACKAGE.equals(processName) && !ALIPAY_PACKAGE_ISHOOK) {
                            ALIPAY_PACKAGE_ISHOOK = true;
                            //注册广播
                            StartAlipayReceived startAlipay = new StartAlipayReceived();
                            IntentFilter intentFilter = new IntentFilter();
                            intentFilter.addAction(PayHelperUtils.ALIPAYSTART_ACTION);
                            context.registerReceiver(startAlipay, intentFilter);
                            PayHelperUtils.sendmsg(context, "支付宝Hook成功，当前支付宝版本:" + PayHelperUtils.getVerName(context));
                            new AlipayHook().hook(appClassLoader, context);

                        }

                    }
                });
            } catch (Throwable e) {
                L.log(e.getMessage());
            }
        }
    }


    //自定义启动支付宝广播
    class StartAlipayReceived extends BroadcastReceiver {
        @SuppressLint("WrongConstant")
        @Override
        public void onReceive(Context context, Intent intent) {
            L.d("收到广播--去爬账单....");
//            AlipayHook.getBillList(context);
        }
    }

}
